package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/** 用户登录功能
 *  程序启动后要求用户输入用户名和密码
 *  然后对比userinfo表中是否存在该用户，如果匹配则提示登录成功
 *  否则提示用户名或密码不正确
 *  登录逻辑：是有用户名密码都正确才算登录成功
 */
public class LoginDemo {
    public static void main(String[] args) {
        UserInfo userinfo = InputUtil.getInputObject(new UserInfo(),"用户登录","登录");
        try(
            Connection con = DBUtil.getConnection()
        ) {
            Statement sm = con.createStatement();
            String sql = "SELECT username,password,nickname " +
                         "FROM userinfo " +
                         "WHERE username='"+userinfo.getUsername()+"' AND password='"+userinfo.getPassword()+"'";
            ResultSet rs = sm.executeQuery(sql);
                if(rs.next()){
                    System.out.println("登录成功!欢迎您，"+rs.getString("nickname"));
                }else {
                    System.out.println("用户名或密码不正确");
                }
        } catch (SQLException e) {
            System.out.println("出现异常了");
        }


    }
}
